/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kmcsoft.goldshop.importdiamond;

import com.kmcsoft.goldshop.importdiamond.service.ImportDiamondService;
import com.kmcsoft.goldshop.model.master.ProductDiamond;
import com.kmcsoft.goldshop.model.master.ProductPremiumJewelry;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zkplus.databind.AnnotateDataBinder;

/**
 *
 * @author AI-LAM
 */
public class AddDiamondModal extends GenericForwardComposer {

    private ProductDiamond selected;
    private ProductPremiumJewelry premiumJewelry;
    @Autowired
    ImportDiamondService importDiamondService;
    private AnnotateDataBinder paBinder;
    private Component component;
    private AnnotateDataBinder binder;

    public ProductDiamond getSelected() {
        return selected;
    }

    public void setSelected(ProductDiamond selected) {
        this.selected = selected;
    }

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        self.setAttribute("diamondModal", this);
        premiumJewelry = (ProductPremiumJewelry) arg.get("selected");
        paBinder = (AnnotateDataBinder) arg.get("binder");
        component = (Component) arg.get("component");
        binder = new AnnotateDataBinder(self);
        self.setAttribute("binder", binder);
        binder.loadAll();
    }

    public void onClick$addDiamond() {
        if (selected != null) {
            selected.setPremiumJewelry(premiumJewelry);
            if (premiumJewelry.getDiamond() != null) {
                premiumJewelry.getDiamond().setPremiumJewelry(null);
                importDiamondService.makePersistent(premiumJewelry.getDiamond());
            }
            premiumJewelry.setDiamond(selected);
            importDiamondService.makePersistent(selected);
            paBinder.loadComponent(component);
        }
        self.detach();
    }

    public void onClick$cancelModal() {
        self.detach();
    }
}
